const app = getApp()
import { geDemandList, getUserInfo } from '../../../api/index'
import { formatTimeTwo } from '../../../utils/util'

Page({
  data: {
    currentPage: 1,
    pageSize: 15,
    list: [],
    showFilterModal: false,
    tabActive: 'demand',
    activeFilter: 0,
    currentFilter: {},
    tabsHeight: 0,
    isEmpty: false,
    keyword: '',
    refresher: false,
    demandFilters: [
      {
        label: '综合',
        value: 0
      }, {
        label: '需求进行中',
        value: 1
      }, {
        label: '已解决',
        value: 2
      }
    ],
    mineFilters: [
      {
        label: '综合',
        value: 0
      }, {
        label: '需求进行中',
        value: 1
      }, {
        label: '已解决',
        value: 2
      }, {
        label: '审核中',
        value: 3
      }, {
        label: '审核不通过',
        value: 4
      }
    ]
  },
  async onRefresh () {
    await this.getList()
    this.setData({ refresher: false })
    // this.setData({ refresher: true })
  },
  showFilter () {
    this.setData({ showFilterModal: true })
  },
  closeFilterModal () {
    this.setData({ showFilterModal: false })
  },
  selectFilter ({ detail }) {
    this.setData({
      currentFilter: detail,
      activeFilter: detail.value,
      showFilterModal: false
    })
    this.getList()
  },
  changeTab ({ currentTarget: { dataset: { value } } }) {
    this.setData({ list: [], tabActive: value, activeFilter: 0, currentFilter: {} })
    this.getList()
  },
  publish () {
    wx.$nav.navigateTo('/pages/demand/edit/index')
  },
  changeSearch ({ detail }) {
    this.setData({ keyword: detail })
    this.getList()
  },
  seeDetail ({ currentTarget: { dataset: { item } } }) {
    wx.$nav.navigateTo(`/pages/demand/detail/index?id=${item.id}`)
  },
  search ({ detail }) {
    this.setData({ keyword: detail })
    this.getList()
  },
  async loadMore () {
    try {
      if (this.loading || this.alloaded) return
      this.loading = true
      let query = {
        currentPage: this.data.currentPage,
        pageSize: this.data.pageSize
      }
      if (this.data.tabActive === 'mine') {
        query.userId = this.data.userInfo.userId
        query.isMine = true
        if (this.data.activeFilter === 1) {
          query.reviewStatus = 2
          query.isFinish = false
        } else if (this.data.activeFilter === 2) {
          query.reviewStatus = 2
          query.isFinish = true
        } else if (this.data.activeFilter === 3) {
          query.reviewStatus = 1
          query.isFinish = false
        } else if (this.data.activeFilter === 4) {
          query.reviewStatus = 3
          query.isFinish = true
        } else {
          query.reviewStatus = ''
          query.isFinish = ''
        }
      } else {
        if (this.data.activeFilter) query.isFinish = this.data.activeFilter === 1 ? false : true
        else query.isFinish = ''
      }
      if (this.data.keyword) query.materialName = this.data.keyword
      if (query.isFinish === '') delete query.isFinish
      if (!query.reviewStatus) delete query.reviewStatus
      const { ResultSet } = await geDemandList({ ...query })
      this.alloaded = !ResultSet.userData.data.length
      this.data.isEmpty = !ResultSet.userData.data.length
      this.setData({ isEmpty: this.data.isEmpty })
      if (ResultSet.userData.data.length) {
        ResultSet.userData.data.forEach(item => {
          item.createTime = formatTimeTwo(item.createTime, 'Y-M-D')
        })
        this.data.list.push(...ResultSet.userData.data)
        this.setData({
          currentPage: this.data.currentPage + 1,
          list: this.data.list
        })
      }
      this.loading = false
    } catch (err) {
      console.log(err)
      this.loading = false
      wx.showToast({ title: err.message || err.ResultSet.errors, icon: 'none' })
    }
  },
  async getList () {
    try {
      wx.showLoading({ title: '加载中...', mask: true })
      let query = {
        currentPage: 1,
        pageSize: this.data.pageSize
      }
      if (this.data.tabActive === 'mine') {
        if (!this.data.userInfo || !this.data.userInfo.userId) {
          wx.$nav.redirectTo(`/pages/login/index`)
          return
        }
        query.isMine = true
        query.userId = this.data.userInfo.userId
        if (this.data.activeFilter === 1) {
          query.reviewStatus = 2
          query.isFinish = false
        } else if (this.data.activeFilter === 2) {
          query.reviewStatus = 2
          query.isFinish = true
        } else if (this.data.activeFilter === 3) {
          query.reviewStatus = 1
          query.isFinish = false
        } else if (this.data.activeFilter === 4) {
          query.reviewStatus = 3
          query.isFinish = true
        } else {
          query.reviewStatus = ''
          query.isFinish = ''
        }
      } else {
        if (this.data.activeFilter) query.isFinish = this.data.activeFilter === 1 ? false : true
        else query.isFinish = ''
      }
      if (this.data.keyword) query.materialName = this.data.keyword
      if (query.isFinish === '') delete query.isFinish
      if (!query.reviewStatus) delete query.reviewStatus
      this.setData({ list: [], isEmpty: false })
      const { ResultSet } = await geDemandList({ ...query })
      this.data.isEmpty = ResultSet.userData.data.length && ResultSet.userData.data.length < this.data.pageSize
      ResultSet.userData.data.forEach(item => {
        item.createTime = formatTimeTwo(item.createTime, 'Y-M-D')
      })
      this.setData({
        list: ResultSet.userData.data,
        currentPage: 2,
        isEmpty: this.data.isEmpty
      })
      wx.hideLoading()
    } catch (err) {
      wx.hideLoading()
      console.log(err)
      wx.showToast({ title: err.message || err.ResultSet.errors, icon: 'none' })
    }
  },
  async getUserInfo () {
    try {
      const { ResultSet } = await getUserInfo({})
      app.globalData.userInfo = ResultSet.userData.userInfo
      this.setData({ userInfo: ResultSet.userData.userInfo })
    } catch (err) {
      wx.showToast({ title: err.message || err.ResultSet.errors })
    }
  },
  async onLoad () {
    await this.getUserInfo()
    let query = this.createSelectorQuery()
    query.select('.demand-tabs').boundingClientRect(rect => {
      this.setData({ tabsHeight: rect.height })
    }).exec()
    // this.getList()
  },
  onShow () {
    this.getList()
  }
})